package com.duowei.diandan.bean;

import android.databinding.BaseObservable;
import android.databinding.Bindable;

import com.duowei.diandan.BR;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 金额
 */
public class Money extends BaseObservable implements Serializable {
    /** 总额 */
    private BigDecimal total = new BigDecimal("0");
    /** 折扣金额 */
    private BigDecimal discount = new BigDecimal("0");
    /** 应收金额 */
    private BigDecimal receivable = new BigDecimal("0");
    /** 已付金额 */
    private BigDecimal paid = new BigDecimal("0");
    /** 未付金额 */
    private BigDecimal unpaid = new BigDecimal("0");

    public Money() {
    }

    public Money(BigDecimal total, BigDecimal discount) {
        this.total = total;
        this.discount = discount;
        compute();
    }

    // 总额

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
        compute();

        notifyPropertyChangedAll();
    }

    @Bindable
    public float getTotalFloat() {
        return total.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public void setTotalFloat(float total) {
        this.total = BigDecimal.valueOf(total);
        compute();

        notifyPropertyChangedAll();
    }

    // 折扣金额

    public BigDecimal getDiscount() {
        return discount;
    }

    public void setDiscount(BigDecimal discount) {
        this.discount = discount;
        compute();

        notifyPropertyChangedAll();
    }

    @Bindable
    public float getDiscountFloat() {
        return discount.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public void setDiscountFloat(float discount) {
        this.discount = BigDecimal.valueOf(discount);
        compute();

        notifyPropertyChangedAll();
    }

    // 应收金额

    public BigDecimal getReceivable() {
        return receivable;
    }

    public void setReceivable(BigDecimal receivable) {
        this.receivable = receivable;
        compute();

        notifyPropertyChangedAll();
    }

    @Bindable
    public float getReceivableFloat() {
        return receivable.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public void setReceivableFloat(float receivable) {
        this.receivable = BigDecimal.valueOf(receivable);
        compute();

        notifyPropertyChangedAll();
    }

    // 已付金额

    public BigDecimal getPaid() {
        return paid;
    }

    public void setPaid(BigDecimal paid) {
        this.paid = paid;
        compute();

        notifyPropertyChangedAll();
    }

    @Bindable
    public float getPaidFloat() {
        return paid.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public void setPaidFloat(float paid) {
        this.paid = BigDecimal.valueOf(paid);
        compute();

        notifyPropertyChangedAll();
    }

    // 未付金额

    public BigDecimal getUnpaid() {
        return unpaid;
    }

    public void setUnpaid(BigDecimal unpaid) {
        this.unpaid = unpaid;

        notifyPropertyChangedAll();
    }

    @Bindable
    public float getUnpaidFloat() {
        return unpaid.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public void setUnpaidFloat(float unpaid) {
        this.unpaid = BigDecimal.valueOf(unpaid);
        compute();

        notifyPropertyChangedAll();
    }

    private void notifyPropertyChangedAll() {
        notifyPropertyChanged(BR.totalFloat);
        notifyPropertyChanged(BR.discountFloat);
        notifyPropertyChanged(BR.receivableFloat);
        notifyPropertyChanged(BR.paidFloat);
        notifyPropertyChanged(BR.unpaidFloat);
    }

    // --

    /**
     * 计算金额
     */
    private void compute() {
        receivable = total.subtract(discount).setScale(2, BigDecimal.ROUND_HALF_UP);
        unpaid = receivable.subtract(paid).setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
